<?

	class Cms_MaintenanceParams extends Core_Configuration_Model
	{
		public $record_code = 'maintenance_configuration';
		
		public static function create()
		{
			$configObj = new Cms_MaintenanceParams();
			return $configObj->load();
		}

		protected function build_form()
		{
			$this->add_field('enabled', 'Enable maintenance mode', 'full', db_number)->renderAs(frm_checkbox)->comment("During the maintenance session visitors will see the page which you select below.", "above");

			$this->add_field('maintenance_page', 'Maintenance notification page', 'full', db_text)->renderAs(frm_dropdown)->cssClassName('checkbox_align')->noLabel();
		}
		
		public function get_maintenance_page_options($key_value = -1)
		{
			$pages = Cms_Page::create()->order('title')->where('security_mode_id <> ?', 'customers')->find_all();
			$result = array();
			foreach ($pages as $page)
				$result[$page->id] = $page->title.'['.$page->url.']';
				
			return $result;
		}
		
		public static function set_status($enabled)
		{
			$obj = self::create();
			if (!$obj->maintenance_page)
				throw new Phpr_ApplicationException('The maintenance mode is not configured');
				
			$obj->enabled = $enabled ? 1 : 0;
			$obj->save();
		}
		
		public function is_configured()
		{
			return $this->maintenance_page ? true : false;
		}
	}

?>